<template>
	<div>elemtUI
		<el-form :model="user" :rules="rules" ref="user"  label-width="100px">
			<el-form-item prop="account" label="账号">
				<el-input v-model="user.account"></el-input>
			</el-form-item>
			<el-form-item label="密码" prop="pwd">
				<el-input show-password v-model="user.pwd"></el-input>
			</el-form-item>
			<el-form-item label="手机号" prop="phone">
				<el-input v-model="user.phone"></el-input>
			</el-form-item>
			<el-form-item label="性别" prop="sex">
				<el-radio-group v-model="user.sex">
					<el-radio label="0">男</el-radio>
					<el-radio label="1">女</el-radio>
					<el-radio label="3">保密</el-radio>
				</el-radio-group>
			</el-form-item>
			<el-form-item label="爱好" prop='hobby'>
				<el-checkbox-group v-model="user.hobby">
					<el-checkbox label="0">睡觉</el-checkbox>
					<el-checkbox label="1">敲代码</el-checkbox>
					<el-checkbox label="2">跑步</el-checkbox>
				</el-checkbox-group>
			</el-form-item>
			<el-form-item label="生日" prop='birthday'>
			<el-date-picker v-model="user.birthday"
			 type="date" 
			 format="yyyy年MM月dd日" 
			 value-format="timestamp"></el-date-picker>
			</el-form-item>
			<el-form-item 
			 label="email"
			 v-for="(item,index) in user.email"
			 :prop="'email['+index+']'"
			 :rules="[{
				 required:true,message:'邮箱不能为空',trigger:'change'
			 },{
				type:'email', message:'邮箱格式不正确',trigger:'change'
			 }]"
			 >
				<el-input v-model="user.email[index]"></el-input>
			  <el-button @click="remove(index)">del</el-button>
			</el-form-item>
			<el-form-item>
				<el-button @click="add">add</el-button>
				<el-button type="success" @click="submitForm('user')">提交</el-button>
				<el-button type="success" @click="resetForm('user')">重置表单</el-button>
			</el-form-item>
		</el-form>

	</div>
</template>

<script>
	//在当前表单中创建mail选项，要求实现动态添加emall元素，
	//新增不得超过5个
	//，删除不能小于一个，提交时还要获取所有emall值
	export default {
		data() {
		let checkAccount=(rule,value,callback)=>{
		     //获取正则
			 let accountReg=/^1[345678]\d{9}$/
			 if(!accountReg.test(value)){
				return  callback(new Error("账号格式不正确"));
			 }else{
				 callback();
			 }
		}
		let pwdCheck=(rule,value,callback)=>{
			 let pwdReg=/^\d{6,}$/;
			 if(!pwdReg.test(value)){
				return callback(new Error("密码不能小于6位数")); 
			 }else{
				  callback();
			 }
		}
		
			return {
				user: {
					account: "",
					pwd: "",
					phone: "",
					sex: null,
					hobby: [],
					email:[""],
					birthday: ""
				},
				rules:{
					account:[{
						required:true,message:"账号不能为空",trigger:"blur"
					},{
					   validator:checkAccount,message:"账号格式不正确",trigger:"change"
					}],
					pwd:[{
						required:true,message:"密码不能为空",trigger:"blur"
					},{
						validator:pwdCheck,trigger:"change"
					}],
					phone:[{
						required:true,message:"手机号不能为空",trigger:"blur"
					},{
						validator:checkAccount,message:"手机号格式不正确",trigger:"change"
					}],
					sex:[{
						required:true,message:"请选择性别",trigger:"change"
					}],
					hobby:[{
						required:true,message:"请选择爱好",trigger:"change"
					}],
					birthday:[{
					 required:true,message:"请选择时间",trigger:"change"	
					}]
				}
			}
		},
		components: {

		},
		methods: {
           submitForm(value){
			   //将校验结果返回的提交按钮
			   this.$refs[value].validate((valid)=>{
				   if(valid){
					    alert('提交成功');
					    console.log(this.user);
					  // this.$refs[value].resetFields();
				   }
			   });
			  
		   },
		   resetForm(value){
			  
			   this.$refs[value].resetFields();
		   },
		   add(){
			   
			   if(this.user.email.length<5){
				   this.user.email.push("");
			   }
		   },
		   remove(index){
			   if(this.user.email.length>1){
			   this.user.email.splice(index,1);
			   }
		   }
		   
		}
	}
</script>

<style>
</style>
